"use strict";

QuickPasswordsMobile.Preferences = {
  // add new default preferences here:
	defaultPREFS : {
		debug: true
  },
	
	get ExtensionBranch() {
    return "extensions.quickpasswordsmobile.";
	},
	
	service: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch),
	
	getBoolPref: function(term) {
	  let eBranch = this.ExtensionBranch + term;
		try {
			return this.service.getBoolPref(eBranch);
		}
		catch(ex) {
			QuickPasswordsMobile.Util.logException("getBoolPref(" + eBranch + ")", ex);
			return false;
		}
	},
	
	setBoolPref: function(term, val) {
		this.service.setBoolPref(this.ExtensionBranch + term, val);
	},
	
	getIntPref: function(p) {
		return this.service.getIntPref(p);
	},

	get isDebug() {
		return this.getBoolPref("debug");
	},

	isDebugOption: function(option) { // granular debugging
		if(!this.isDebug) return false;
		try {return this.getBoolPref("debug." + option);}
		catch(e) {return false;}
	},
	
  setDefaultPrefs : function() {
    let branch = Services.prefs.getDefaultBranch(this.PreferencesBranch);
    for (let [key, val] in Iterator( MenuOnTop.defaultPREFS )) {
		  MenuOnTop.Util.logDebug ("Loading default pref " + key + ": " + val);
		
      switch (typeof val) {
        case "boolean":
          branch.setBoolPref(key, val);
          break;
        case "number":
          branch.setIntPref(key, val);
          break;
        case "string":
          branch.setCharPref(key, val);
          break;
      }
    }
  }	

}
